1679B - Stone Age Problem - CodeForces Solution


data structures implementation *1200

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline
n, q = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
marker = {}
for i in range(n):
    marker[i+1] = a[i]
x = 0
for _ in range(q):
    k = list(map(int, input().split()))
    if k[0] == 1:
        s += k[2] - marker.get(k[1], x)
        marker[k[1]] = k[2]
    else:
        marker.clear()
        s = k[1] * n
        x = k[1]
    print(s)

C++ Code:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;

int main(){
	ll n, q;
	cin >> n >> q;
	ll sum = 0;
	map <int, pair<int, int>> m; //m[index] = {time, value}
	// m[n] = {all changed time, value}
	for(int i = 0; i < n; ++i){
		ll a;
		cin >> a;
		sum += a;
		m[i] = {-1, a};
	}
	
	m[n] = {-1, 0};
	
	for(int i = 0; i < q; ++i){
		int t;
		cin >> t;
		if(t == 1){
			int ix, x;
			cin >> ix >> x;
			--ix;
			auto [time, value] = m[ix];
			auto [allChangedTime, allChangedValue] = m[n];
			
			if(time < allChangedTime){
				sum -= allChangedValue;
			}else sum -= value;
			
			sum += x;
			m[ix] = {i, x};
		}else{
			int x;
			cin >> x;
			sum = n * x;
			m[n] = {i, x};
		}
		cout << sum << "\n";
	}
	return 0;
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do something instead of nothing and stay organized
*/


Comments

Submit
0 Comments
More Questions

678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams